package zm.irc.cmd;

import java.util.Map;
import zm.irc.client.IrcChannel;
import zm.irc.client.IrcClient;
import zm.irc.ioc.annotation.AutoWired;
import zm.irc.ioc.annotation.Service;
import zm.irc.message.receive.IrcReceiveCmdMessage;
import zm.irc.message.send.IrcChatMessage;
import zm.irc.util.CommonUtil;

/**
 * <pre>
 * This command is used to part target channel.
 * Usage :
 * zlang ListChannel
 * </pre>
 */
@Service(name=CommandNameConst.CMD_LIST_JOINED)
public class CmdListJoinedChannels implements IrcChatMsgCmd{

    @AutoWired
    private CommonUtil commonUtil;

    /**
     * <pre>
     * 1. Build {@link IrcChatMessage} base on channel name.
     * 2. Send those Part Message out.
     * </pre>
     */
    @Override
    public boolean execute(IrcReceiveCmdMessage cmd, IrcClient client) {

        Map<String,IrcChannel> allChannels = client.getAllChannel();
        if(allChannels == null || allChannels.isEmpty()){
            return true;
        }
        IrcChannel targetChannel = client.getChannel(cmd.getChannel());

        allChannels.forEach((key,channel)->{
            String msgStr = String.format(" >> %s : Currently, I'm Listening on :%s",cmd.getFromName(),key );
            targetChannel.send(msgStr);
            commonUtil.slowDown(800);
        });

        return true;
    }


}
